# coding=utf-8
"""
    @project: flask-demo
    @Author：frank
    @file： Run.py
    @date：2023/8/13 10:10
"""
from flask import Flask, url_for, render_template

#  将当前运行的主程序构建成Flask应用，以便接收用户的请求(request)并给出响应(response)
app = Flask(__name__)


# @app.route() Flask中的路由定义，定义用户的访问路径
@app.route('/index')
@app.route('/')
def index():
    return "this  is my first flask app"


@app.route('/login')
def login():
    return '这是登录页面'


@app.route('/register')
def register():
    return '这是注册页面'


@app.route('/show1/<name>')
def show1(name):
    return "<h1>姓名为:%s</h1>" % name


@app.route('/show2/<name>/<age>')
def show2(name, age):
    return "<h1>姓名为:%s,年龄为:%s</h1>" % (name, age)


# age为整型，并非字符串
@app.route('/show3/<name>/<int:age>')
def show3(name, age):
    return '传递进来的参数是name:%s,age:%d' % (name, age)


@app.route('/post', methods=['POST'])
def post():
    return '这个post请求'


@app.route('/url')
def url_views():
    # login_url = url_for('login')
    # return "<a href='%s'>登录入口</a>" % login_url
    url = url_for('show2', name='Frank', age=28)
    return "<a href='%s'>show2入口</a>" % url


@app.route('/tmp_view')
def template_views():
    hi = 'hello frank'
    title = '《钢铁是怎么练成的》'
    author = '奥斯特洛夫斯基'
    price = 32.5
    publisher = '北京大学出版社'
    params = locals()
    print(params)
    return render_template('index.html', params=params)


@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html')

if __name__ == "__main__":
    # 运行Flask应用（启动Flask的服务）默认在本机的端口号5000
    app.run(debug=True)
